;; header file for 8 bit series assembly -*- mode:asm; coding:utf-8; -*-
;; author    Ting
;; date      2020-12-24
;;
;; # constant definition
;;
;; ## instruction result store destination
;;  - W: the result is stored in the ACC.
;;  - F: the result is stored back in register.
;; ## flag name in STATUS register
;;  - C, DC, Z
;;
;; # macro define
;;
;; ## RETURN, used by C compiler, end of function
;; ## PAGESEL, empty macro, maybe used by C compiler (bug?)
;; ## BANKSEL, replace by .banksel pseudo command, empty if disable bank.
;; ## MGOTO, replace by LGOTO (ic<=2K word) or FGOTO (ic>2K word)
;; ## MCALL, same as above
;;
W       EQU 0
F       EQU 1
C		EQU	0
DC		EQU	1
Z		EQU	2


;; common register for all series
INDF    EQU 0x00
PCL     EQU 0x02
STATUS  EQU 0x03
FSR     EQU 0x04
PCHBUF  EQU 0x0A

;; ## return from subroutine
;;
RETURN      macro
    ret
    endm

PAGESEL macro rom_address
    endm


IFNDEF USEFGOTO
    USEFGOTO EQU 0
ENDIF

IF (USEFGOTO) ; {

MGOTO macro rom_address
    FGOTO rom_address
    endm
MCALL macro rom_address
    FCALL rom_address
    endm

ELSE ; } {

MGOTO macro rom_address
    LGOTO rom_address
    endm
MCALL macro rom_address
    LCALL rom_address
    endm

ENDIF ; }

;; # RAM bank select
;;
;; select RAM bank.
;; 這邊是一個小魔法，能用ram_address 的 relocation 資訊組合出不同的instruction (BCR/BSR)
;; 這樣可以在不更動Acc的狀況下切換 ram bank
;;
;; 8A 系列, BK[1:0] 為 FSR[7:6]
;; 8B 系列, BK[1:0] 為 STATUS[7:6]
;;
;; argument
;;  - address: RAM address
;;
;; # remark
;;
;; bcr: 0x1c00
;; bsr: 0x1800
;;
;; 0 1   1 1 b b   b r r r   r r r r (14bit)
;; \_____/ | \_____/ \_____________/
;;    |    |    |           |
;;    |    |    |           +------> register address
;;    |    |    +------------------> FSR[7:6] are used as Bank register BK[1:0] (@8A)
;;    |    |                         STATUS[7:6] are used as Bank register BK[1:0] (@8B)
;;    |    +-----------------------> 0: set bit (bsr).
;;    |                              1: clear bit (bcr).
;;    +----------------------------> instruction bsr/bcr
;;
;; 這邊使用 .banksel 虛指令，assembler會填入特殊的relocation type，
;; linker 必須根據不同的IC Body產生對應的 bcr/bsr 指令。
;; 這件事情由linker負責，好處是不需要事先提供各種ic body
;; 對應的的static library。
;;/

IFNDEF ENABLE_RAM_BANK
    ENABLE_RAM_BANK EQU 0
ENDIF

IF (ENABLE_RAM_BANK) ; {
BANKSEL macro ram_address
    .banksel ram_address
endm
ELSE ; } { disable Banksel
BANKSEL macro ram_address
endm
ENDIF ; ENABLE_RAM_BANK }
